package com.ruoyi.psi.purchase.apply.service;


import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.psi.purchase.apply.domain.PurchaseApply;
import com.ruoyi.psi.purchase.apply.domain.PurchaseApplyList;
import com.ruoyi.psi.purchase.apply.mapper.PurchaseApplyMapper;
import com.ruoyi.utils.ID.IdUtil;
import com.ruoyi.utils.UtilsList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * 采购申请单
 * 业务处理层
 * @author Drj 2021/6/28
 * 修改：lsy 2021.7.17
 */
@Service
public class PurchaseApplyServiceImpl implements IPurchaseApplyService{
    @Autowired
    private PurchaseApplyMapper purchaseApplyMapper;
    @Autowired
    private IdUtil idUtil;

    /**
     * 采购申请单查询
     *
     * @param purchaseApply
     * @return
     */
    @Override
    public List<PurchaseApply> selectPurchaseApplyList(PurchaseApply purchaseApply) {
        List<PurchaseApply> purchaseApplies = purchaseApplyMapper.selectPurchaseApplyList(purchaseApply);
        for (int i = 0; i < purchaseApplies.size(); i++) {
            if (StringUtils.isNotNull(purchaseApplyMapper.selectPurchaseApplyListListById(purchaseApplies.get(i).getPurchaseApplyId()))){
                //存在，则存入采购申请单对应产品信息
                purchaseApplies.get(i).setPurchaseApplyLists(purchaseApplyMapper.selectPurchaseApplyListListById(purchaseApplies.get(i).getPurchaseApplyId()));
            }
        }
        return purchaseApplies;
    }

    /**
     * 按编号查询
     *
     * @param purchaseApplyId 采购申请单编号
     * @return
     */
    @Override
    public PurchaseApply selectPurchaseApplyById(String purchaseApplyId) {
        return purchaseApplyMapper.selectPurchaseApplyById(purchaseApplyId);
    }

    /**
     * 采购申请单新增
     *
     * @param purchaseApply
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertPurchaseApply(PurchaseApply purchaseApply) throws Exception{
        String purchaseApplyId = idUtil.createPurchaseApplyId();//自动生成编号
        if (StringUtils.isNotNull(purchaseApplyMapper.selectPurchaseApplyById(purchaseApplyId))){
            //存在
            return  0;
        }
        //不存在
        purchaseApply.setPurchaseApplyId(purchaseApplyId); // 主表添加编号
        purchaseApplyMapper.insertPurchaseApply(purchaseApply);//采购申请单据
        for (int i = 0; i < purchaseApply.getPurchaseApplyLists().size(); i++) {
            purchaseApply.getPurchaseApplyLists().get(i).setPurchaseApplyId(purchaseApplyId);// 子表添加编号
            purchaseApplyMapper.insertPurchaseApplyList(purchaseApply.getPurchaseApplyLists().get(i));//采购申请产品信息
        }
        return 1;
    }

    /**
     * 采购申请单更新
     *
     * @param purchaseApply
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updatePurchaseApply(PurchaseApply purchaseApply) throws Exception{
        if (!StringUtils.isNotNull(purchaseApplyMapper.selectPurchaseApplyById(purchaseApply.getPurchaseApplyId()))){
            //不存在
            return  0;
        }
        //存在采购申请单据
        purchaseApplyMapper.updatePurchaseApply(purchaseApply);//采购申请单据
        List<PurchaseApplyList> purchaseApplyLists1 = purchaseApplyMapper.selectPurchaseApplyListListById(purchaseApply.getPurchaseApplyId());
        List<PurchaseApplyList> purchaseApplyLists = purchaseApply.getPurchaseApplyLists();
        for (int i = 0; i < purchaseApplyLists.size(); i++) {
            PurchaseApplyList purchaseApplyList = purchaseApplyMapper.selectPurchaseApplyListById(purchaseApply.getPurchaseApplyId(),purchaseApplyLists.get(i).getProduceId());
            if (StringUtils.isNotNull(purchaseApplyList)){
                //存在采购申请产品
                for (int j = 0; j < purchaseApplyLists1.size(); j++) {
                    if (purchaseApplyLists1.get(j).getProduceId().equals(purchaseApplyList.getProduceId())){
                        purchaseApplyLists1.remove(j);//移除新旧都有的部分
                        break;
                    }
                }//更新
                purchaseApplyMapper.updatePurchaseApplyList(purchaseApplyLists.get(i));
            }else{//新增
                purchaseApplyMapper.insertPurchaseApplyList(purchaseApplyLists.get(i));
            }
        }
        for (int i = 0; i < purchaseApplyLists1.size(); i++) {
            //删除新旧重叠以外的部分
            purchaseApplyMapper.deletePurchaseApplyListById(purchaseApplyLists1.get(i).getPurchaseApplyId(),purchaseApplyLists1.get(i).getProduceId());
        }
        return 1;
    }

    /**
     * 更新采购申请单删除标志位
     *
     * @param purchaseApply
     * @return
     */
    @Override
    public int updatePurchaseApplyDeleteFlag(PurchaseApply purchaseApply) {
        return purchaseApplyMapper.updatePurchaseApplyDeleteFlag(purchaseApply);
    }

    /**
     * 采购申请单详细查询
     *
     * @param purchaseApplyList
     * @return
     */
    @Override
    public List<PurchaseApplyList> selectPurchaseApplyListList(PurchaseApplyList purchaseApplyList) {
        return purchaseApplyMapper.selectPurchaseApplyListList(purchaseApplyList);
    }

    /**
     * 查询采购申请单对应的所有产品详细
     *
     * @param purchaseApplyId 采购申请单编号
     * @return
     */
    @Override
    public List<PurchaseApplyList> selectPurchaseApplyListListById(String purchaseApplyId) {
        return purchaseApplyMapper.selectPurchaseApplyListListById(purchaseApplyId);
    }

    /**
     * 查询采购申请单对应的指定产品详细
     *
     * @param purchaseApplyId
     * @param produceId
     * @return
     */
    @Override
    public PurchaseApplyList selectPurchaseApplyListById(String purchaseApplyId, String produceId) {
        return purchaseApplyMapper.selectPurchaseApplyListById(purchaseApplyId,produceId);
    }


    /**
     * 采购申请单详细新增
     *
     * @param purchaseApplyList
     * @return
     */
    @Override
    public int insertPurchaseApplyList(PurchaseApplyList purchaseApplyList) {
        return purchaseApplyMapper.insertPurchaseApplyList(purchaseApplyList);
    }

    /**
     * 采购申请单详细更新
     *
     * @param purchaseApplyList
     * @return
     */
    @Override
    public int updatePurchaseApplyList(PurchaseApplyList purchaseApplyList) {
        return purchaseApplyMapper.updatePurchaseApplyList(purchaseApplyList);
    }

    /**
     * 删除采购申请单详细
     *
     * @param purchaseApplyId 采购申请单编号
     * @param produceId       产品编号
     * @return
     */
    @Override
    public int deletePurchaseApplyListById(String purchaseApplyId, String produceId) {
        return purchaseApplyMapper.deletePurchaseApplyListById(purchaseApplyId,produceId);
    }

    /**
     * 获取最大的采购申请单编号
     *
     * @return
     */
    @Override
    public String selectMaxPurchaseApplyId() {
        return purchaseApplyMapper.selectMaxPurchaseApplyId();
    }
}
